using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Beta_fundies_controls_quotes_Submit : System.Web.UI.UserControl
{
    protected void CustomRequiredField(object source, ServerValidateEventArgs args)
    {
        args.IsValid = args.Value.Trim().Length > 0;
    }
    protected void ValidateUrl(object source, ServerValidateEventArgs args)
    {
        args.IsValid = Uri.IsWellFormedUriString(args.Value, UriKind.Absolute);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlArchive.DataSource = DAL.Archives.All();
            ddlArchive.DataBind();

            if (ddlArchive.Items.Count > 0)
            {
                ddlArchive.SelectedValue = "1";
            }

            FillContributor();
        }
        Page.Title = "FSTDT! Submit a Quote";
    }

    private void FillContributor()
    {
        txtContributor.Text = ContributorSettings.Contributor;
        txtContributorEmail.Text = ContributorSettings.ContributorEmail;
        if (ContributorSettings.CreditContributor == true)
        {
            lstCreditContributor.SelectedValue = "Yes";
        }
        else
        {
            lstCreditContributor.SelectedValue = "No";
        }
    }

    private void SaveContributor()
    {
        ContributorSettings.Contributor = txtContributor.Text;
        ContributorSettings.ContributorEmail = txtContributorEmail.Text;
        ContributorSettings.CreditContributor = lstCreditContributor.SelectedValue.ToLower().Equals("yes");
    }   

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DAL.Quote myQuote = new DAL.Quote();
            myQuote.Contributor = txtContributor.Text;
            myQuote.ContributorEmail = txtContributor.Text;
            myQuote.CreditContributor = lstCreditContributor.SelectedValue.ToLower().Equals("yes");

            myQuote.Fundie = txtFundie.Text.Trim();
            myQuote.FundieQuote = txtQuote.Text.Trim();
            myQuote.Board = txtBoard.Text.Trim();
            myQuote.URL = txtURL.Text.Trim();

            myQuote.Archive = DAL.Archives.Item(Convert.ToInt64(ddlArchive.SelectedValue));

            DAL.Quotes.Add(myQuote);

            SaveContributor();
            
            mviewSubmit.SetActiveView(viewThanks);

            ClearFields();
        }
    }

    private void ClearFields()
    {
        txtContributor.Text = "";
        txtContributorEmail.Text = "";
        txtFundie.Text = "";
        txtQuote.Text = "";
        txtBoard.Text = "";
        txtURL.Text = "";
    }
    protected void ValidateArchive(object source, ServerValidateEventArgs args)
    {
        long id;
        if (Int64.TryParse(args.Value, out id))
        {
            args.IsValid = DAL.Archives.Item(id) != null;
        }
        else
        {
            args.IsValid = false;
        }
    }

    private static class ContributorSettings
    {
        public static bool CreditContributor
        {
            get
            {
                if (DAL.CookieManager.TryGetCookie("CreditContributor", "true").Equals("true"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            set
            {
                if (value == true)
                {
                    DAL.CookieManager.SetCookie("CreditContributor", "true", DateTime.Now.AddYears(1));
                }
                else
                {
                    DAL.CookieManager.SetCookie("CreditContributor", "false", DateTime.Now.AddYears(1));
                }
            }
        }

        public static string Contributor
        {
            get
            {
                return DAL.CookieManager.TryGetCookie("Contributor", "");
            }
            set
            {
                DAL.CookieManager.SetCookie("Contributor", value, DateTime.Now.AddYears(1));
            }
        }

        public static string ContributorEmail
        {
            get
            {
                return DAL.CookieManager.TryGetCookie("ContributorEmail", "");
            }
            set
            {
                DAL.CookieManager.SetCookie("ContributorEmail", value, DateTime.Now.AddYears(1));
            }
        }
    }
}
